﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Shopfloormanagement.Forms
{
    public partial class Form_LayoutParameters : Form
    {
        public string FormType = "";
        int t = 0;
        string name1;
        string name2;
        public Form_LayoutParameters()
        {
            InitializeComponent();
        }
        private void btnEnter1_Click(object sender, EventArgs e)
        {
            if (t == 0)
            {
                name1 = txtType1.Text + "#" + cmbName1.Text + "#" + txtControlName1.Text + "#" + cmbRowCount1.Text + "#" + txtRow1.Text + "#" + txtRow2.Text + "#" + txtMax1.Text + "#" + txtMin1.Text + "#" + txtIndicator1.Text + "#" + txtTarget1.Text;
                if (cmbName1.Text == "红绿表格")
                {
                    if (txtControlName1.Text != "")
                    {
                        if (txtRow1.Text != "")
                        {
                            if (txtIndicator1.Text != "")
                            {
                                if (txtTarget1.Text != "")
                                {
                                    DialogResult dr = MessageBox.Show("红绿表格是否需要搭配文本表格?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                                    if (dr == DialogResult.Yes)
                                    {
                                        //搭配文本表格
                                        string name3 = txtType1.Text + "#文本表格#" + txtControlName1.Text + "_Comment#-1#####" + txtIndicator1.Text + "#" + txtTarget1.Text;
                                        Form_Layout.frm.AddRGtable2(name1, name3);
                                        this.Close();
                                    }
                                    else
                                    {
                                        //不搭配文本表格
                                        Form_Layout.frm.AddRGtable(name1, Form_Layout.frm.panel1);
                                        this.Close();
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("请输入目标/极限！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtTarget1.Focus();
                                }
                            }
                            else
                            {
                                MessageBox.Show("请输入标识！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                txtIndicator1.Focus();
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入第一行标题！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtRow1.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show("请输入控件名称！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtControlName1.Focus();
                    }
                }
                else if (cmbName1.Text == "文字表格")
                {
                    if (txtControlName1.Text != "")
                    {
                        if (cmbRowCount1.Text != "")
                        {
                            if (cmbRowCount1.Text == "1")
                            {
                                if (txtRow1.Text == "")
                                {
                                    MessageBox.Show("请输入第一行标题！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtRow1.Focus();
                                    return;
                                }
                            }
                            else if (cmbRowCount1.Text == "2")
                            {
                                if (txtRow1.Text == "")
                                {
                                    MessageBox.Show("请输入第一行标题！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtRow1.Focus();
                                    return;
                                }
                                else if (txtRow2.Text == "")
                                {
                                    MessageBox.Show("请输入第二行标题！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtRow2.Focus();
                                    return;
                                }
                            }
                            if (txtMax1.Text != "")
                            {
                                if (txtMin1.Text != "")
                                {
                                    if (txtIndicator1.Text != "")
                                    {
                                        if (txtTarget1.Text != "")
                                        {
                                            Form_Layout.frm.AddCharactertable(name1, Form_Layout.frm.panel1);
                                            this.Close();
                                        }
                                        else
                                        {
                                            MessageBox.Show("请输入目标/极限！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            txtTarget1.Focus();
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("请输入标识！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        txtIndicator1.Focus();
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("请输入下限值！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtMin1.Focus();
                                }
                            }
                            else
                            {
                                MessageBox.Show("请输入上限值！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                txtMax1.Focus();
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入行数！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            cmbRowCount1.DroppedDown = true;
                        }
                    }
                    else
                    {
                        MessageBox.Show("请输入控件名称！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtControlName1.Focus();
                    }
                }
                else if (cmbName1.Text == "折线图")
                {
                    if (txtControlName1.Text != "")
                    {
                        if (txtMax1.Text != "")
                        {
                            if (txtMin1.Text != "")
                            {
                                if (txtIndicator1.Text != "")
                                {
                                    if (txtTarget1.Text != "")
                                    {
                                        MessageBox.Show("折线图需搭配其他控件使用，请设置第二个控件参数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        cmbName1.Items.RemoveAt(3);
                                        cmbName1.Enabled = true;
                                        cmbName1.SelectedIndex = -1;
                                        cmbRowCount1.SelectedIndex = -1;
                                        txtRow1.Text = "";
                                        txtRow2.Text = "";
                                        txtMax1.Text = "";
                                        txtMin1.Text = "";
                                        txtIndicator1.Text = "";
                                        txtTarget1.Text = "";
                                        t = 1;
                                        cmbName1.DroppedDown = true;
                                    }
                                    else
                                    {
                                        MessageBox.Show("请输入目标/极限！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        txtTarget1.Focus();
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("请输入标识！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtIndicator1.Focus();
                                }
                            }
                            else
                            {
                                MessageBox.Show("请输入下限值！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                txtMin1.Focus();
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入上限值！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtMax1.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show("请输入控件名称！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtControlName1.Focus();
                    }
                }
            }
            else
            {
                name2 = txtType1.Text + "#" + cmbName1.Text + "#" + txtControlName1.Text + "#" + cmbRowCount1.Text + "#" + txtRow1.Text + "#" + txtRow2.Text + "#" + txtMax1.Text + "#" + txtMin1.Text + "#" + txtIndicator1.Text + "#" + txtTarget1.Text;
                if (cmbName1.Text == "红绿表格")
                {
                    if (txtControlName1.Text != "")
                    {
                        if (txtRow1.Text != "")
                        {
                            if (txtIndicator1.Text != "")
                            {
                                if (txtTarget1.Text != "")
                                {
                                    Form_Layout.frm.AddLineChart(name1, name2);
                                    this.Close();
                                }
                                else
                                {
                                    MessageBox.Show("请输入目标/极限！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtTarget1.Focus();
                                }
                            }
                            else
                            {
                                MessageBox.Show("请输入上限值！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                txtMax1.Focus();
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入第一行标题！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtRow1.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show("请输入控件名称！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtControlName1.Focus();
                    }
                }
                else if (cmbName1.Text == "文字表格")
                {
                    if (txtControlName1.Text != "")
                    {
                        if (cmbRowCount1.Text != "")
                        {
                            if (cmbRowCount1.Text == "1")
                            {
                                if (txtRow1.Text == "")
                                {
                                    MessageBox.Show("请输入第一行标题！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtRow1.Focus();
                                    return;
                                }
                            }
                            else if (cmbRowCount1.Text == "2")
                            {
                                if (txtRow1.Text == "")
                                {
                                    MessageBox.Show("请输入第一行标题！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtRow1.Focus();
                                    return;
                                }
                                else if (txtRow2.Text == "")
                                {
                                    MessageBox.Show("请输入第二行标题！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtRow2.Focus();
                                    return;
                                }
                            }
                            if (txtMax1.Text != "")
                            {
                                if (txtMin1.Text != "")
                                {
                                    if (txtIndicator1.Text != "")
                                    {
                                        if (txtTarget1.Text != "")
                                        {
                                            Form_Layout.frm.AddLineChart(name1, name2);
                                            this.Close();
                                        }
                                        else
                                        {
                                            MessageBox.Show("请输入目标/极限！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            txtTarget1.Focus();
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("请输入标识！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        txtIndicator1.Focus();
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("请输入下限值！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    txtMin1.Focus();
                                }
                            }
                            else
                            {
                                MessageBox.Show("请输入上限值！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                txtMax1.Focus();
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入行数！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            cmbRowCount1.DroppedDown = true;
                        }
                    }
                    else
                    {
                        MessageBox.Show("请输入控件名称！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtControlName1.Focus();
                    }
                }
                else if (cmbName1.Text == "文本表格")
                {
                    if (txtControlName1.Text != "")
                    {
                        Form_Layout.frm.AddLineChart(name1, name2);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("请输入控件名称！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtControlName1.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("请选择控件类型！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void btnCancel1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void cmbName1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbName1.Text == "红绿表格")
            {
                cmbRowCount1.SelectedIndex = 1;
                cmbRowCount1.Enabled = false;
                txtRow1.Text = "";
                txtRow2.Text = "";
                txtMax1.Text = "";
                txtMin1.Text = "";
                txtIndicator1.Text = "";
                txtTarget1.Text = "";
                txtRow1.Enabled = true;
                txtRow2.Enabled = false;
                txtMax1.Enabled = false;
                txtMin1.Enabled = false;
                txtIndicator1.Enabled = true;
                txtTarget1.Enabled = true;
            }
            else if (cmbName1.Text == "文字表格")
            {
                cmbRowCount1.SelectedIndex = -1;
                cmbRowCount1.Enabled = true;
                txtRow1.Text = "";
                txtRow2.Text = "";
                txtMax1.Text = "";
                txtMin1.Text = "";
                txtIndicator1.Text = "";
                txtTarget1.Text = "";
                txtRow1.Enabled = true;
                txtRow2.Enabled = true;
                txtMax1.Enabled = true;
                txtMin1.Enabled = true;
                txtIndicator1.Enabled = true;
                txtTarget1.Enabled = true;
            }
            else if (cmbName1.Text == "折线图")
            {
                cmbRowCount1.SelectedIndex = 0;
                cmbRowCount1.Enabled = false;
                txtRow1.Text = "";
                txtRow2.Text = "";
                txtMax1.Text = "";
                txtMin1.Text = "";
                txtIndicator1.Text = "";
                txtTarget1.Text = "";
                txtRow1.Enabled = false;
                txtRow2.Enabled = false;
                txtMax1.Enabled = true;
                txtMin1.Enabled = true;
                txtIndicator1.Enabled = true;
                txtTarget1.Enabled = true;
            }
            else if (cmbName1.Text == "文本表格")
            {
                cmbRowCount1.SelectedIndex = 0;
                cmbRowCount1.Enabled = false;
                txtRow1.Text = "";
                txtRow2.Text = "";
                txtMax1.Text = "";
                txtMin1.Text = "";
                txtIndicator1.Text = "";
                txtTarget1.Text = "";
                txtRow1.Enabled = false;
                txtRow2.Enabled = false;
                txtMax1.Enabled = false;
                txtMin1.Enabled = false;
                txtIndicator1.Enabled = false;
                txtTarget1.Enabled = false;
            }
        }
        private void cmbRowCount1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbRowCount1.SelectedIndex == 0)
            {
                txtRow1.Text = "";
                txtRow2.Text = "";
                txtRow1.Enabled = false;
                txtRow2.Enabled = false;

            }
            else if (cmbRowCount1.SelectedIndex == 1)
            {
                txtRow1.Text = "";
                txtRow2.Text = "";
                txtRow1.Enabled = true;
                txtRow2.Enabled = false;

            }
            else if (cmbRowCount1.SelectedIndex == 2)
            {
                txtRow1.Text = "";
                txtRow2.Text = "";
                txtRow1.Enabled = true;
                txtRow2.Enabled = true;
            }
        }
        private void txtMax1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 允许退格键  
            if (e.KeyChar == (char)Keys.Back)
            {
                return; // 不做任何处理，允许退格键操作  
            }
            // 允许输入的字符包括数字0-9和点号  
            char ch = e.KeyChar;
            if (!Char.IsDigit(ch) && ch != '.')
            {
                // 如果不是数字也不是点号，则阻止按键事件  
                e.Handled = true;
            }
            // 可选：如果需要限制小数点只能出现一次  
            // 获取TextBox的当前文本  
            string text = ((TextBox)sender).Text;
            // 检查是否已存在小数点，并且用户没有尝试删除它  
            if (ch == '.' && text.Contains(".") && !e.KeyChar.Equals(Keys.Back))
            {
                e.Handled = true;
            }

        }
        private void txtMin1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 允许退格键  
            if (e.KeyChar == (char)Keys.Back)
            {
                return; // 不做任何处理，允许退格键操作  
            }
            // 允许输入的字符包括数字0-9和点号  
            char ch = e.KeyChar;
            if (!Char.IsDigit(ch) && ch != '.')
            {
                // 如果不是数字也不是点号，则阻止按键事件  
                e.Handled = true;
            }
            // 可选：如果需要限制小数点只能出现一次  
            // 获取TextBox的当前文本  
            string text = ((TextBox)sender).Text;
            // 检查是否已存在小数点，并且用户没有尝试删除它  
            if (ch == '.' && text.Contains(".") && !e.KeyChar.Equals(Keys.Back))
            {
                e.Handled = true;
            }

        }
    }
}